package string.leetcode.medium;

import java.util.Arrays;

/**
 * @author bruin_du
 * @description 字符串中的变位词
 * @date 2022/8/4 16:43
 **/
public class OfferII014_CheckInclusion {
    public boolean checkInclusion(String s1, String s2) {
        int n = s1.length(), m = s2.length();
        if (n > m) {
            return false;
        }
        int[] cnt1 = new int[26];
        int[] cnt2 = new int[26];
        for (int i = 0; i < n; ++i) {
            ++cnt1[s1.charAt(i) - 'a'];
            ++cnt2[s2.charAt(i) - 'a'];
        }
        if (Arrays.equals(cnt1, cnt2)) {
            return true;
        }
        for (int i = n; i < m; ++i) {
            ++cnt2[s2.charAt(i) - 'a'];
            --cnt2[s2.charAt(i - n) - 'a'];
            if (Arrays.equals(cnt1, cnt2)) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        OfferII014_CheckInclusion offer = new OfferII014_CheckInclusion();
        System.out.println(offer.checkInclusion("ab", "eidbaooo"));
    }
}
